package com.github.pig.admin.controller.appCommon;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pig.admin.model.app.AppMessageShow;
import com.github.pig.admin.model.entity.SysFile;
import com.github.pig.admin.service.app.IAppMessageShowService;
import com.github.pig.admin.service.appCommon.AppService;
import com.github.pig.admin.service.sysFile.SysFileService;
import com.github.pig.admin.util.AjaxJson;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.util.ResponseUtil;
import com.github.pig.common.web.BaseController;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * app通用接口
 */
@RestController
@EnableAutoConfiguration
public class AppController extends BaseController {

    @Autowired
    private AppService appService;
    @Autowired
    private IAppMessageShowService appMessageShowService;
    @Autowired
    private SysFileService sysFileService;

    private final ResourceLoader resourceLoader;

    @Autowired
    public AppController(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    /**
     * 发送手机验证码
     *
     * @param mobile 手机号
     * @return R
     */
    @GetMapping("/appCode")
    public ResponseUtil createCode(String mobile) {
        ResponseUtil responseUtil = new ResponseUtil();
        if (StringUtils.isBlank(mobile)){
            responseUtil.setStatus(ResponseUtil.ERROR);
            responseUtil.setMsg("手机号不能为空");
            return responseUtil;
        }
        return appService.sendSmsCode(mobile);
    }

    /**
     * 上传文件
     * @return success/false
     */
    @PostMapping("/upload")
    public ResponseUtil uploadFile(@RequestBody Map<String,Object> param) {
        ResponseUtil responseUtil = new ResponseUtil();
        //当前登陆名
        String loginName = getUserName();
        if (org.apache.commons.lang3.StringUtils.isBlank(loginName)) {
            responseUtil.setStatus(ResponseUtil.ERROR);
            responseUtil.setMsg("请先登录！");
            return responseUtil;
        }
         responseUtil = appService.uploadFile(param,loginName);
        return responseUtil;
    }

    /**
     * 获取轮播图
     *return
     */
    @RequestMapping("/showLbtApp")
    public AjaxJson showImg(String communityId) {
        AjaxJson json = new AjaxJson();
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.eq("community_id",communityId);
        entityWrapper.eq("del_flag", CommonConstant.STATUS_NORMAL);
        AppMessageShow appMessageShow = appMessageShowService.selectOne(entityWrapper);
        List<String> list = new ArrayList<>();
        EntityWrapper fileWrapper = new EntityWrapper();
        fileWrapper.eq("relation_id",appMessageShow==null?"cs":appMessageShow.getId());
        fileWrapper.eq("del_flag",CommonConstant.STATUS_NORMAL);
        //获取文件列表
        List<SysFile> sysFiles = sysFileService.selectList(fileWrapper);
        if (sysFiles.size() > 0){
            for (SysFile sysFile : sysFiles) {
                list.add(sysFile.getDocPath());
            }
            json.setSuccess(true);
            json.setObj(list);
        }else {
            json.setSuccess(false);
        }
        return json;
    }

    /**
     * 微信登陆-绑定openid和手机号
     *
     * @param mobile 手机号
     * @return R
     */
    @RequestMapping("/bindOpenId")
    public AjaxJson bindOpenId(String openId,String mobile,String code) {
        AjaxJson json = new AjaxJson();
        json.setSuccess(true);
        if (StringUtils.isBlank(mobile)){
            json.setSuccess(false);
            json.setMsg("手机号不能为空");
            return json;
        }
        if (StringUtils.isBlank(code)){
            json.setSuccess(false);
            json.setMsg("验证码不能为空");
            return json;
        }
        if(StringUtils.isBlank(openId)){
            json.setSuccess(false);
            json.setMsg("微信信息获取失败");
            return json;
        }
        return appService.bindOpenId(openId,mobile,code,json);
    }

    /**
     * 微信绑定openid
     *
     * @param userId 手机号
     * @return R
     */
    @RequestMapping("/vxBindOpenId")
    public AjaxJson bindOpenId(String openId,int userId) {
        AjaxJson json = new AjaxJson();
        json.setSuccess(true);
        return appService.bindOpenId(openId,userId,json);
    }

    /**
     * 微信登陆-解绑openid和手机号
     *
     * @return R
     */
    @RequestMapping("/unBindOpenId")
    public AjaxJson unBindOpenId() {
        AjaxJson json = new AjaxJson();
        json.setSuccess(true);
        //当前登陆名
        Claims claims = getCurrentUserInfo();
        String loginName = (String) claims.get("user_name");
        appService.unBindOpenId(loginName);
        json.setMsg("解绑成功!");
        return json;
    }

}
